package com.xinjie.www.service.thumb;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.dao.thumb.ThumbDao;
import com.xinjie.www.dao.thumb.ThumbDaoImpl;
import com.xinjie.www.po.Collect;
import com.xinjie.www.po.Thumb;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.xinjie.www.dao.BaseDao.getConnection;

public class ThumbServiceImpl implements ThumbService {
    private final ThumbDao thumbDao;

    public ThumbServiceImpl() {
        thumbDao = new ThumbDaoImpl();
    }

    @Override
    public boolean doThumb(int postId, int userId, String time) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        conn = getConnection();
        boolean flag = false;
        if (conn != null) {
            int i = thumbDao.doThumb(conn, postId, userId, time);
            if (i > 0) {
                flag = true;
            }
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    @Override
    public List<Thumb> getThumb(int userId) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        conn = BaseDao.getConnection();
        List<Thumb> thumbs = new ArrayList<>();
        if (conn != null) {
            thumbs = thumbDao.getThumb(conn, userId);
            BaseDao.closeResource(conn, null, null);
        }
        return thumbs;
    }

    @Override
    public boolean cancelThumb(int thumbId, int postId) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        conn = BaseDao.getConnection();
        boolean flag = false;
        if (conn != null) {
            int i = thumbDao.cancelThumb(conn, thumbId, postId);
            if (i > 0) {
                flag = true;
            }
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }
}
